# -*- coding:utf-8 -*-

from __future__ import unicode_literals

from collections import OrderedDict
from operator import itemgetter

from flask import Flask, render_template, request, redirect, url_for, g, session, flash
from peewee import Model, SqliteDatabase, CharField, ForeignKeyField, PrimaryKeyField

from modelos import Professor, Disciplina, Aluno, AlunoDisciplina, Aula, AlunoAula, Atividade
from modelos import conexao

from login_manager import login_required, login, logout, form_add_professor
from disciplina_manager import ver_disciplina, remover_disciplina, form_add_disciplinas, vincular_aluno
from aluno_manager import excluir_aluno, form_add_alunos, desvincular_aluno
from aula_manager import form_add_aula, form_add_aula_salva, excluir_aula
from atividade_manager import form_add_atividade

from __init__ import app

DEBUG = True

@app.before_request
def before_request():
    g.db = conexao
    g.db.connect()

    if "user_id" in session:
        g.user_id = session["user_id"]
    else:
        g.user_id = None

@app.after_request
def after_request(response):
    g.db.close()
    return response

@app.route("/")
@login_required
def index():

    p = Professor.get(Professor.id == session["user_id"])

    disciplinas = Disciplina.select().where(Disciplina.professor == p)

    return render_template("index.html", disciplinas=disciplinas)     

if __name__ == "__main__":
    app.debug = True
    app.run()